package com.cyt.web.controller.phone;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.ErrorCode;
import com.cyt.web.controller.base.PhoneController;
import com.cyt.work.divideclass.dao.DivideclassMessageDao;
import com.cyt.work.divideclass.model.po.DivideclassMessage;
import com.cyt.work.divideclass.model.po.DivideclassStudent;
import com.cyt.work.divideclass.model.vo.DivideClassClassInoVo;
import com.cyt.work.divideclass.model.vo.DivideclassMessageVo;
import com.cyt.work.divideclass.model.vo.DivideclassStudentVo;
import com.cyt.work.divideclass.service.DivideclassStudentService;
import com.cyt.wx.model.po.WxEducationUser;
import com.cyt.wx.model.po.WxMember;
import com.cyt.wx.model.vo.WxEducationUserVo;
import com.cyt.wx.service.WxEducationUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 智能分班--移动端api接口
 *
 * @author caideyong
 * @date 2019-11-26
 */
@RestController
public class PhoneDivideClassController extends PhoneController {

    @Autowired
    private DivideclassStudentService divideclassStudentService;
    @Autowired
    private WxEducationUserService wxEducationUserService;
    @Autowired
    private DivideclassMessageDao divideclassMessageDao;


    /**
     * 登录页面
     * @param token
     * @param applyId
     * @author han jun
     * @date 2020/1/6 10:01
     */
    @RequestMapping(value = "/phone/divideclass/divideclassLogin")
    public ModelAndView questionLogin(String token, String applyId) {
        ModelAndView modelAndView = new ModelAndView("/mobile/divideclass/divideClassLogin");
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        return modelAndView;
    }

    /**
     * 登录
     * @param token
     * @param applyId
     * @author caideyong
     * @date 2020/1/4 10:55
     */
    @RequestMapping(value = "/phone/divideclass/login",method = RequestMethod.POST)
    public ReturnResult login(String token, String applyId, String loginName, String password){
        WxMember wxMember = acceptToken(token);
        return wxEducationUserService.login(wxMember, loginName, password);
    }

    /**
     * 分班详情界面
     * @param token
     * @param applyId
     * @author caideyong
     * @date 2020/1/4 08:50
     */
    @RequestMapping(value = "/phone/divideclass/divideclassDetail")
    public ModelAndView divideclassDetail(String token, String applyId) {
        // 校验
        Map<String, Object> map = acceptToken(token, applyId, APPLY_TYPE_FB);
        if (!(Boolean) map.get("flag")) {
            return (ModelAndView) map.get("modelAndView");
        }
        WxMember wxMember = acceptToken(token);
        WxEducationUserVo wxEducationUserVo = new WxEducationUserVo();
        wxEducationUserVo.setMemberId(wxMember.getId());
        List<WxEducationUser> wxEducationUserList = wxEducationUserService.listExample(wxEducationUserVo);
        //能登录进去就可以直接使用
        WxEducationUser wxEducationUser = wxEducationUserList.get(0);
        ModelAndView modelAndView =new ModelAndView("mobile/divideclass/divideClassDetail");
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        DivideclassMessageVo divideclassMessageVo=new DivideclassMessageVo();
        divideclassMessageVo.setDivideId(wxEducationUser.getExamine());
        divideclassMessageVo.setUserId(wxEducationUser.getId());
        List<DivideclassMessage> divideclassMessageList=divideclassMessageDao.getDivideMessageList(divideclassMessageVo);
        List<DivideClassClassInoVo> classInfoList = new ArrayList<>();
        if(CollUtil.isNotEmpty(divideclassMessageList)){
            for(DivideclassMessage item:divideclassMessageList){
                DivideClassClassInoVo vo = new DivideClassClassInoVo();
                vo.setTitle(item.getClassName());
                vo.setValue(item.getClassId());
                classInfoList.add(vo);
            }
        }
        modelAndView.addObject("classList",JSON.toJSONString(classInfoList));
        return modelAndView;
    }

    /**
     * 根据classId查询学生信息
     *
     * @param divideclassStudentVo 传入参数对象(classId 必传;name,school为搜索关键字非必填)
     * @author caideyong
     * @date 2019/12/31 08:50
     */
    @RequestMapping(value = "/phone/divideclassStudent/getDivideclassStudent")
    public ReturnResult getDivideclassStudent(DivideclassStudentVo divideclassStudentVo) {
        List<DivideclassStudent> divideclassStudentList = divideclassStudentService.listExample(divideclassStudentVo);
        if (divideclassStudentList != null && divideclassStudentList.size() > 0) {
            return new ReturnResult(divideclassStudentList);
        } else {
            return new ReturnResult(ErrorCode.CODE_1, "未查询到该班级的学生！");
        }
    }
}